package org.testis.data.parser;

import org.testis.communication.data.CommunicationModuleDataObject;
import org.testis.data.TestStepDataObject;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import java.util.LinkedHashMap;
import java.util.regex.Pattern;


/**
 * Defines a class to parse data for communication module
 * @author nurhat
 */
public class CommunicationModuleDop implements TestStepDop
{

    /**
     * Parsing data and creating a CommunicationModuleDataObject
     * Returns null if any required attribute not given
     * @param data
     * @return
     */
    public TestStepDataObject convert(TestStepDataObject data)
    {
        CommunicationModuleDataObject dataObj =
            new CommunicationModuleDataObject(data);
        LinkedHashMap<String, String> map = data.getDataToApply();

        String host = map.get(AtofModuleParameterNames.CM_HOST);
        String userName = map.get(AtofModuleParameterNames.CM_USERNAME);
        String password = map.get(AtofModuleParameterNames.CM_PASSWORD);
        String port = map.get(AtofModuleParameterNames.CM_PORT);
        String commandToSend = map.get(
            AtofModuleParameterNames.CM_COMMAND_TO_SEND);
        String stringToSendAndVerify = map.get(
            AtofModuleParameterNames.CM_STRING_TO_SEND_AND_VERIFY);
        String keepAliveTime = map.get(
            AtofModuleParameterNames.CM_KEEP_ALIVE_TIME);
        String commandSeperatorForTelnetClient = map.get(
            AtofModuleParameterNames.CM_COMMAND_PROMPT_CHAR);
        dataObj.setHost(host);
        dataObj.setUserName(userName);
        dataObj.setPassword(password);
        if (commandSeperatorForTelnetClient != null) {
            if (!commandSeperatorForTelnetClient.equals("")) {
                dataObj.setCommandPromptCharForTelnetClient(
                    commandSeperatorForTelnetClient);
            }
        }
        if (port != null) {
            boolean isInteger = Pattern.matches("^\\d*$",
                                                port);
            if (isInteger) {
                dataObj.setPort(Integer.parseInt(port));
            }
            else {
                LogSenderService.putMessageOnQueue("CommunicationModuleDop",
                                                   AtofLogLevel.ERROR,
                                                   "CommunicationModule Data Object is not " +
                                                   "correctly given. " +
                                                   AtofModuleParameterNames.CM_KEEP_ALIVE_TIME +
                                                   " must be integer ",
                                                   data.getMessageId());
                return null;
            }
        }
        dataObj.setCommandToSend(commandToSend);
        dataObj.setStringToSendAndVerify(stringToSendAndVerify);
        if (keepAliveTime != null) {
            boolean isInteger = Pattern.matches("^\\d*$",
                                                keepAliveTime);
            if (isInteger) {
                dataObj.setKeepAliveTime(Integer.parseInt(keepAliveTime));
            }
            else {
                LogSenderService.putMessageOnQueue("CommunicationModuleDop",
                                                   AtofLogLevel.ERROR,
                                                   "CommunicationModule " +
                                                   "Data Object is not correctly given." +
                                                   AtofModuleParameterNames.CM_KEEP_ALIVE_TIME +
                                                   " must be integer ",
                                                   data.getMessageId());
                return null;
            }
        }
        return dataObj;

    }
}
